package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    //  远程调用的feign接口 来进行查询
    @Autowired
    private SkuFeign skuFeign;


    @Autowired
    private ESManagerMapper esManagerMapper;

    //创建索引库
    @Override
    public void createMappingAndIndex() {
        //需要基于ES的模板类来创建
        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);


        //创建映射   --  实体类
        elasticsearchTemplate.putMapping(SkuInfo.class);

    }


    //  导入全部sku集合进入索引库
    @Override
    @SuppressWarnings("all")
    public void importAll() {
        // 查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");

        if (skuList == null || skuList.size()<0 ){
            //  代表当前没有查询到数据

            throw new RuntimeException("当前没有数据被查询到，无法导入索引库");
        }

        //有数据
        //把skuList 数据转换为json 数据
        String jsonSkuList = JSON.toJSONString(skuList);

        //将json 转换skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            // 将规格信息转换为map

            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);

            skuInfo.setSpecMap(specMap);

        }


        //导入索引库
        esManagerMapper.saveAll(skuInfoList);


    }


    //  根据spuid查询skuList  ，添加到索引库中
    @Override
    public void importDataBySpuId(String spuId) {
        //查询所有的
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);

        if (skuList == null || skuList.size()<0 ){
            //  代表当前没有查询到数据

            throw new RuntimeException("当前没有数据被查询到，无法导入索引库");
        }

        // 将集合转换为json
        String jsonSkuList = JSON.toJSONString(skuList);

        //将json 转换skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            // 将规格信息转换为map

            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);

            skuInfo.setSpecMap(specMap);

        }


        //导入索引库
        esManagerMapper.saveAll(skuInfoList);


    }



    /**
     *
     * @param spuId
     */
    @Override
    public void delDateBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size()<0){
            throw new RuntimeException("当前没有数据被查询到，无法导入索引库");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));

        }

    }
}
